package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.entity.monitoringData;
import com.qiyeji.real_estate.entity.monitoringDataKey;
import com.qiyeji.real_estate.service.MonitoringDataService;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/MonitoringData")
@RestController
@ResponseBody
public class MonitoringDataController {
    @Autowired
    MonitoringDataService monitoringDataService;

    @PostMapping("/getAllMonitoringData")
    public Result getAllMonitoringData() {
        List<monitoringData> monitoringData = monitoringDataService.getAllMonitoringData();
        if(monitoringData==null) {
            return Result.error();
        }
        else {
            return Result.success(monitoringData);
        }
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody monitoringData monitoringData){
        int res=monitoringDataService.insert(monitoringData);
        if(res==0) {
            return Result.error();
        }
        else {
            return Result.success(res);
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody monitoringDataKey key){
        int res=monitoringDataService.delete(key);
        if(res==0){
            return Result.error();
        }else{
            return Result.success(res);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody monitoringData monitoringData){
        int res=monitoringDataService.updateByPrimaryKey(monitoringData);
        if(res==0){
            return Result.error();
        }else{
            return Result.success(res);
        }
    }

    @PostMapping("/selectById")
    public Result selectById(@RequestBody monitoringDataKey key){
        monitoringData monitoringData=monitoringDataService.selectByPrimaryKey(key);
        if(monitoringData==null){
            return Result.error();
        }else{
            return Result.success(monitoringData);
        }
    }

    @PostMapping("/fuzzyQuery")
    public Result fuzzyQuery(@RequestBody Map<String,String> params){
        String key = params.get("key");
        List<monitoringData> monitoringData = monitoringDataService.fuzzyQuery(key);
        if(monitoringData==null) {
            return Result.error();
        }
        else {
            return Result.success(monitoringData);
        }
    }
}
